package com.ocom.xfrecode.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.ocom.common.entity.card.EntityXfGoodListDishes;
import com.ocom.common.request.payconfig.mealorder.GetOrderDishesRequest;
import com.ocom.common.vo.xfrecode.GetPointMealSendVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface XfGoodListDishesMapper extends BaseMapper<EntityXfGoodListDishes> {

    String getOrderDishesCSql =
            " from xf_goods_list  g " +
            " left join xf_record  o on  g.order_no = o.order_no  and g.com_id = o.com_id  " +
            " where  " +
            " g.com_id = #{comId}  " +
            " and  g.month_value = #{monthValue} " +
            " and  o.month_value = #{monthValue} " +
            " <when test=\"receiptName != null\" > " +
            "   AND g.receipt_name like  CONCAT(CONCAT('%', #{receiptName}), '%')    " +
            " </when> " +
            " <when test=\"merchId != null\" > " +
            "   AND g.merch_id = #{merchId} " +
            " </when> " +
            " <when test=\"mealNo != null\" > " +
            "   AND g.meal_no = #{mealNo} " +
            " </when> " +
            " <when test=\"pointId != null\" > " +
            "   AND g.point_id = #{pointId} " +
            " </when> " +
            " <when test=\"orderNo != null\" > " +
            "   AND g.order_no = #{orderNo} " +
            " </when> " +
            " <when test=\"ifRefund != -1\" > " +
            "   AND g.if_refund = #{ifRefund} " +
            " </when> " +
            " <when test=\"remark != null\" > " +
            "   AND o.reserved3  like  CONCAT(CONCAT('%', #{remark}), '%')    " +
            " </when> " +
            " <when test=\"deptId != null\" > " +
            "   AND o.dept_id = #{deptId} " +
            " </when> " +
            " <when test=\"receiptIds.size()>0 \" > "+
            " and g.receipt_id in" +
            "<foreach collection='receiptIds' item='item' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>"+
            " </when> "+
            " <when test=\"pName != null \">" +
            "   AND o.p_name  like  CONCAT(CONCAT('%', #{pName}), '%')    " +
            " </when>"+
            "  and       g.meal_date <![CDATA[ >= ]]> #{beginDate}  \n" +
            "  and       g.meal_date <![CDATA[ <= ]]> #{endDate}  \n" +
            " order by g.meal_date desc , g.meal_no desc, g.cons_time desc " ;


    @Select({"<script>",
            "SELECT g.* , o.p_name as pName , o.p_code as pCode , o.reserved3 as remark, o.cons_money as  amount," +
                    " o.merch_name as merchName , o.point_name as pointName , o.device_name as  deviceName , o.discount as discount , o.real_money as realMoney  " +
             getOrderDishesCSql +
                    " LIMIT #{size} OFFSET #{page}  "+
                    "</script>"})
    List<EntityXfGoodListDishes> getOrderDishesC(GetOrderDishesRequest request);

    @Select({"<script>",
            "SELECT count(1) " +
                    getOrderDishesCSql  +
                    "</script>"})
    Integer getOrderDishesCCount(GetOrderDishesRequest request);


    @Select({"<script>",
            "SELECT o.point_id ,  o.meal_date ,  o.meal_no , o.receipt_id , o.receipt_name , g.dishes_id , g.dishes_name, " +
                    "sum(case when cons_state in (3,5) then 1  else 0 end ) as  dcTimes," +//dcTimes
                    "sum(case when cons_state=4 then 1  else 0 end ) as  qcTimes," + //qcTimes
                    "sum(case when cons_state=6 then 1  else 0 end ) as  tcTimes " + //tcTimes
                    " from xf_goods_list  g " +
                    " left join xf_record  o on  g.order_no = o.order_no  and g.com_id = o.com_id  " +
                    "  ${ew.customSqlSegment}  "+
                    "</script>"})
    List<GetPointMealSendVo> getPointMealSend(@Param(Constants.WRAPPER) Wrapper wrapper);

}
